package com.zbl.utils;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import net.sourceforge.tess4j.util.LoadLibs;

import java.io.File;

/**
 * @Author: zhangjun
 * @Date: 2020/7/31 18:34
 * @Description:
 */
public class ImageUtils {

    public static String getImgContent(String pathname) {
        String content = "";
        File imageFile = new File(pathname);

        //读取图片数字
        ITesseract instance = new Tesseract();

        File tessDataFolder = LoadLibs.extractTessResources("tessdata");
        instance.setLanguage("eng");//英文库识别数字比较准确
        instance.setDatapath(tessDataFolder.getAbsolutePath());

        try {
            content = instance.doOCR(imageFile).replace("\n", "");
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }
        return content;
    }
}
